package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.easyViewHolder.TuijianHolder;
import com.dingwei.superiordiary.bean.GoodsListBean;
import com.dingwei.superiordiary.bean.Index_Bean;
import com.dingwei.superiordiary.bean.VerietyShopBean;
import com.dingwei.superiordiary.ui.activity.CommodityDetailsActivity;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/10.
 */

public class VarietyShopAdapter extends RecyclerArrayAdapter<VerietyShopBean.DataBean> {

    public interface xxInterFace {
        void  xx(int position);
    }
    private xxInterFace xxInterfaceListener;
        public void xxInterfaceListener(xxInterFace listener){
            xxInterfaceListener = listener;

        }



    public VarietyShopAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new VarietyShopHolder(parent);
    }

    class VarietyShopHolder extends BaseViewHolder<VerietyShopBean.DataBean> {

        @Bind(R.id.tv_type_name)
        TextView tvTypeName;
        @Bind(R.id.tv_type_introduce)
        TextView tvTypeIntroduce;
        @Bind(R.id.tv_more)
        TextView tvMore;
        @Bind(R.id.m_recyclerView)
        RecyclerView mRecyclerView;

        public VarietyShopHolder(ViewGroup parent) {
            super(parent, R.layout.itrm_variety_shop);
            ButterKnife.bind(this, itemView);
            LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRecyclerView.setLayoutManager(layoutManager);
            mRecyclerView.setAdapter(tuijianAdapter);
            mRecyclerView.setNestedScrollingEnabled(false);
            mRecyclerView.addItemDecoration(new DividerDecoration(BaseActivity.context.getResources().getColor(R.color.white), DisplayUtil.dip2px(BaseActivity.context,16)));

            tuijianAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Intent intent  = new Intent(BaseActivity.context, CommodityDetailsActivity.class);
                    VerietyShopBean.DataBean.ProductsBean bean = (VerietyShopBean.DataBean.ProductsBean) tuijianAdapter.getItem(position);
                    intent.putExtra("id",bean.id);
                    BaseActivity.context.startActivity(intent);
                }
            });
        }

        RecyclerArrayAdapter tuijianAdapter = new RecyclerArrayAdapter(BaseActivity.context) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new TuijianHolder(parent);
            }
        };

        @Override
        public void setData(VerietyShopBean.DataBean data) {
            super.setData(data);
            tvTypeName.setText(data.name);
            tvTypeIntroduce.setText(data.description);

            tuijianAdapter.clear();
            tuijianAdapter.addAll(data.products);

            tvMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (xxInterfaceListener!=null){
                        xxInterfaceListener.xx(getDataPosition());
                    }
                }
            });
        }
    }
}
